<?php
class ViewAction extends Action {
	public function homework() {
		if (isset($_SESSION['id'])) {
			$hw_id = $_GET['hw_id'];
			$homeworkinfo_dao = M('homeworkinfo');
			$condition['id'] = $hw_id;
			$homeworkinfo_result = $homeworkinfo_dao -> where($condition) -> find();


			$student_dao = M('student');
			$student_condition;
			$student_condition['id'] = $_SESSION['id'];
			$student_result = $student_dao -> where($student_condition) -> find();

            $group_dao = M('group');
			$group_condition;
			$group_condition['id'] = $student_result['group_id'];

			$group_result = $group_dao->where($group_condition)->find();
			$group_id = $student_result['group_id'];

			$group_condition;
			$group_condition2['group_id'] = $group_id;
			$members = $student_dao -> where($group_condition2) -> select();
            

			$stuhomework_dao = M('stuhomework');
			$members_hw;
			foreach ($members as $key => $member) {
				$stuhomework_condition;
				$stuhomework_condition['hw_id'] = $hw_id;
				$stuhomework_condition['stu_id'] = $member['id'];
				$members_hw[] = $stuhomework_dao -> where($stuhomework_condition) -> find();
				$members_hw[$key]['name'] = $member['name'];
				$members_hw[$key]['studentid'] = $member['studentid'];
				
			}
			$this -> assign('homework', $homeworkinfo_result);
			$this -> assign('memembers', $members);
			$this -> assign('members_hw', $members_hw);
			$this -> assign('student', $student_result);
			$this -> assign('group', $group_result);
			$this -> display();
			

		} else {
			$this -> error("请先登录");
		}

	}

	public function upload() {


		$student_dao = M("student");
		$condition['id'] = $_SESSION['id'];
		$result = $student_dao -> where($condition) -> find();
		if ($result['group_id'] == null) {
			$this -> error("请先加入小组");
		}

		$userfile_name = $_FILES['hw']['name'];
		$userfile_tmp = $_FILES['hw']['tmp_name'];
		$userfile_size = $_FILES['hw']['size'];
		$filename = basename($_FILES['hw']['name']);
		$file_ext = substr($filename, strrpos($filename, '.') + 1);
		$file_ext = strtolower($file_ext);
		if (!in_array($file_ext, array("zip"))) {
			$this -> error("错误：文件类型不匹配");
			return;
		} else {
			$hw_id = $_POST["hw_id"];
			$stu_id = $_SESSION["id"];
		    $homeinfo_dao = M('homeworkinfo');
			$homeinfo_condition;
			$homeinfo_condition['id'] = $hw_id;
			$homework_result = $homeinfo_dao->where($homeinfo_condition)->find();
			$deadline = $homework_result['deadline'];
			if($deadline < time())
			{
				$this->error("该作业已过截止时间");
			}
			//$large_file_name = uniqid () . "." . $file_ext;
			//$large_file_location = C ( './File/' ) . $large_file_name;
			//move_uploaded_file ( $userfile_tmp, "./File/");
			//move_uploaded_file($userfile_tmp, C ("__ROOT__/File").$userfile_name);

			$path = getcwd();
			//获取当前系统目录
			$dest_file = "File/upload_homework/" . $hw_id . "/" . $stu_id;
			if (!is_dir($dest_file)) {
				mk_dir($dest_file);
			}
			$serialid = uniqid();
			$userfile_name = $serialid . "." . $file_ext;
			move_uploaded_file($userfile_tmp, $dest_file . "/" . $userfile_name);

			//$zip_handler = zip_open($dest_file."/".$userfile_name);
			$zip = new ZipArchive();
			$zip -> open($dest_file . "/" . $userfile_name);
			$directory = $zip -> getNameIndex(1);
			$zip -> extractTo($dest_file);
			$zip -> close();

			$stuhomework_dao = M("stuhomework");
			$stuhomework_condition;
			$stuhomework_condition['hw_id'] = $_POST['hw_id'];
			$stuhomework_condition['stu_id'] = $_SESSION['id'];
			$data['hw_id'] = $_POST['hw_id'];
			$data['stu_id'] = $_SESSION['id'];
			$data['link'] = $dest_file . "/" . $directory . "/";
			$data['file_location'] = $dest_file . "/" . $userfile_name;
			$result = $stuhomework_dao ->where($stuhomework_condition) ->find();
			if($result == null){
				$stuhomework_dao->add($data);
				$this -> success("作业上传成功");
			}
			else{
				$result = $stuhomework_dao -> where($stuhomework_condition) -> save($data);
			if ($result !== false)
				$this -> success("作业上传成功");
			}
			
		}
	}

	public function submitResults() {
		$hw_id = $_GET['hw_id'];
		
		$student_dao = M('student');
		$student_condition;
		$student_condition['id'] = $_SESSION['id'];
		$student_result = $student_dao->where($student_condition)->find();
		$student_condition2;
		$student_condition2['group_id'] = $student_result['group_id'];
		$members = $student_dao->where($student_condition2)->select();
		$this->assign("members", $members);
		$this->assign("hw_id", $hw_id);
		$this->display();

	}
	
	public function submit(){
		$hw_id = $_POST['hw_id'];
		$members = $_POST['members'];

       // print_r($members); 
		/*$stuhomework_dao = M('stuhomework');
		foreach ($members as $key => $value) {
			$condition['stu_id'] = $value['id'];
			$condition['hw_id'] = $hw_id;
			$data[rating] = $_POST[$value['id']];
			$stuhomework_dao->where($condition)->save($data);
		}
		$this->redirect("View/homework?hw_id=".$hw_id);*/
		$stuhomework_dao = M('stuhomework');
		$i = 1;
		foreach ($_POST['rating'] as $key => $value) {
		    
			
			$condition['stu_id'] = $key;
			
			$condition['hw_id'] = $hw_id;
			$data['rating'] = $value;

          
            echo $condition['stu_id'];
			echo $condition['hw_id'];
			$stuhomework_dao->where($condition)->save($data);
			$this->redirect("View/homework?hw_id=".$hw_id);
		}
	}

}
?>